package com.yanyeori.framework.security.database;

import com.yanyeori.framework.datasource.annotation.DataSource;
import com.yanyeori.framework.datasource.util.JdbcNamedParamUtil;
import com.yanyeori.framework.security.bo.InterPermsBO;
import com.yanyeori.framework.security.bo.InterfaceLimiterBO;
import com.yanyeori.framework.security.enums.EnableEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 接口数据库Dao
 *
 * @author chenkuan 2020/12/11
 */
@Repository
@DataSource("security")
public class InterfaceDao {

    @Autowired
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    /**
     * 查询所有用户接口权限
     */
    public List<InterPermsBO> listUserInterPerms() {
        String sql = "SELECT ru.user_id, i.back_url " +
                "FROM t_role_user ru " +
                "LEFT JOIN t_interface_role ir ON ru.role_id = ir.role_id " +
                "LEFT JOIN t_interface i ON ir.interface_id = i.id " +
                "WHERE ir.enabled = :enabled " +
                "AND i.anno_visit = :annoVisit";
        JdbcNamedParamUtil param = JdbcNamedParamUtil.newMap()
                .put("enabled", EnableEnum.ENABLE.getCode())
                .put("annoVisit", EnableEnum.DISABLE.getCode());
        return namedParameterJdbcTemplate.query(sql, param, new BeanPropertyRowMapper<>(InterPermsBO.class));
    }

    /**
     * 查询指定用户接口权限
     */
    public List<InterPermsBO> listUserInterPermsByUser(String userId) {
        String sql = "SELECT ru.user_id, i.back_url " +
                "FROM t_role_user ru " +
                "LEFT JOIN t_interface_role ir ON ru.role_id = ir.role_id " +
                "LEFT JOIN t_interface i ON ir.interface_id = i.id " +
                "WHERE ir.enabled = :enabled " +
                "AND ru.user_id = :userId " +
                "AND i.anno_visit = :annoVisit";
        JdbcNamedParamUtil param = JdbcNamedParamUtil.newMap()
                .put("enabled", EnableEnum.ENABLE.getCode())
                .put("userId", userId)
                .put("annoVisit", EnableEnum.DISABLE.getCode());
        return namedParameterJdbcTemplate.query(sql, param, new BeanPropertyRowMapper<>(InterPermsBO.class));
    }

    /**
     * 查询所有接口、ip限流配置
     */
    public List<InterfaceLimiterBO> listLimiters() {
        String sql = "SELECT i.back_url, il.ip, il.permits_per, il.enabled " +
                "FROM t_interface_limiter il " +
                "LEFT JOIN t_interface i ON il.interface_id = i.id ";
        return namedParameterJdbcTemplate.query(sql, new BeanPropertyRowMapper<>(InterfaceLimiterBO.class));
    }

    /**
     * 加载所有匿名接口
     */
    public List<String> listAnnoInterfaces() {
        String sql = "SELECT i.back_url " +
                "FROM t_interface i " +
                "WHERE i.anno_visit = :annoVisit ";
        JdbcNamedParamUtil param = JdbcNamedParamUtil.newMap()
                .put("annoVisit", EnableEnum.ENABLE.getCode());
        return namedParameterJdbcTemplate.queryForList(sql, param, String.class);
    }
}
